﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace DZDPSpider
{
    public class BaiduMapHelper
    {
        public static string GetAddress(string address)
        {
            string url = @"http://api.map.baidu.com/geocoder/v2/?address="+address+"&output=json&ak=yxHQxmEcNtWl3PF7dMjY9SownEAmGERj";
            string str = HttpHelper.Get(url);
            JObject obj = (JObject)JsonConvert.DeserializeObject(str);

            // 请求百度地图api返回的状态码 0表示ok
            int status = (int)obj["status"];

            if (status == 0)
            {
                // 经度
                double longitude = (double) obj["result"]["location"]["lng"];

                // 纬度
                double latitude = (double) obj["result"]["location"]["lat"];

                return (longitude + "," + latitude);
            }
            else
            {
                return ("地址太长或格式不正确,地址太长或格式不正确");
            }
           
        }
    }
}
